package com.syj.collect.http;



import android.util.Log;

import com.syj.collect.utils.LogUtils;
import com.syj.collect.utils.SPUtil;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;

public class InterceptorUtil {
    public static final String TAG="------";
    public static HttpLoggingInterceptor LogInterceptor() {     //日志拦截器,用于打印返回请求的结果
        return new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
//                LogUtils.i("Interceptor="+message);
            }
        }).setLevel(HttpLoggingInterceptor.Level.BODY);
    }

    public static Interceptor HeaderInterceptor(){      //头部添加请求头信息
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request=chain.request();
                HttpUrl url = chain.request().url();

                if (!url.toString().contains("login")){
                    String token = SPUtil.getPrefString("token", "");
                    Request updateRequest=request.newBuilder().
                    addHeader("token",token).addHeader("Accept-Encoding","Accept-Encoding").build();
//                    Log.e("Interceptor",updateRequest.headers().toString());
                    return chain.proceed(updateRequest);
                }




                return chain.proceed(request);
            }
        };
    }

}
